package cn.stacey.blog.web.demo;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;

import cn.stacey.blog.domain.AjaxResponse;
import cn.stacey.blog.domain.Category;
import cn.stacey.blog.service.CategoryService;
import cn.stacey.blog.utils.ServiceProxyUtils;


//servlet映射
@WebServlet(
		urlPatterns= {"/category/findAll","/category/insert","/category/delete","/category/update"}
		)
public class CategoryServlet extends BaseServlet{
	//CategoryService service=new CategoryService();
	//动态代理获取CategoryService
	CategoryService service =(CategoryService) ServiceProxyUtils.createProxy(new CategoryService());
	public void findAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
		//

		List<Category> data = service.findAll();
		//响应数据的格式
//		 {
//			  code: 0,
//			  msg: "",
//			  count: 1000,
//			  data: []
//			} 
		//在ajax对象中定义了code msg  count
//		AjaxResponse<Category> ajax = new AjaxResponse<>();
//		ajax.setCode(0);
//		ajax.setMsg("");
//		ajax.setData(data);
//		String string = JSON.toJSONString(ajax);
//		resp.setContentType("application/json;charset=utf-8");
//		resp.getWriter().write(string);
//		System.out.println("findAll");
		writeListData(resp, data);
	}
	
	
	public void insert(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {	
		String categoryName = req.getParameter("name");
		Category category = new Category();
		category.setName(categoryName);
		category.setPriority(2);
		service.insertCategory(category);
		
		//返回一个简单的响应
		AjaxResponse reponse = new AjaxResponse();
		reponse.setCode(0);
		reponse.setMsg("success");
		String string = JSON.toJSONString(reponse);
		resp.setContentType("application/json;charset=utf-8");
		resp.getWriter().write(string);
		System.out.println("insert");
	}


	
	public void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
		int id =Integer.valueOf(req.getParameter("id"));	
		//处理与外键关联不能删除的列
		
		//返回一个响应成功的消息
				AjaxResponse ajax=new AjaxResponse();
		try {
			service.deleteCategory(id);
			//如果不报错就响应成功的消息
			ajax.setCode(0);
			ajax.setMsg("success");
		}catch(Exception e){
			System.out.println(e);
			//不正常则返回数据库操作失败的消息
			ajax.setCode(1);
			ajax.setMsg("数据库操作失败!");
		}
		String string = JSON.toJSONString(ajax);
		resp.setContentType("application/json;charset=utf-8");
		resp.getWriter().write(string);
	}
	
	public void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
	CategoryService service = new CategoryService();
		
		int id= Integer.valueOf(req.getParameter("id"));
		String name=req.getParameter("name");

		String priority=req.getParameter("priority");	
		Category category = new Category();
		category.setId(id);
		category.setName(name);
		if(priority!=null) {
			int p =Integer.valueOf(priority);
			category.setPriority(p);
		}
		
		
		service.updateCategory(category);
		
		//返回一个简单的响应
		AjaxResponse reponse = new AjaxResponse();
		reponse.setCode(0);
		reponse.setMsg("success");
		String string = JSON.toJSONString(reponse);
		resp.setContentType("application/json;charset=utf-8");
		resp.getWriter().write(string);
	}
	
}
